package com.cat.dataStructure;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/search-a-2d-matrix-ii/?envType=study-plan-v2&envId=top-100-liked
 * @create 2025/9/25 21:26
 * @since JDK17
 */

public class Solution81 {
    public boolean searchMatrix(int[][] matrix, int target) {
        int i = 0, n = matrix.length, j = matrix[0].length - 1;
        while (i < n && j >= 0) {
            if (matrix[i][j] == target) {
                return true;
            }
            if (matrix[i][j] < target) {    // 这一行不会存在答案
                i++;
            } else {    // 这一列中不会存在答案
                j--;
            }
        }
        return false;
    }
}
